package com.zhihao.takeaway.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.entity.*;
import com.zhihao.takeaway.mapper.OrdersMapper;
import com.zhihao.takeaway.mapper.UserMapper;
import com.zhihao.takeaway.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;


@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
        implements OrdersService{

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public EasyUITable getOrdersByCondition(Integer currentNum,String employeeId,Integer status,Integer wantNum,String id) {
        List<BookAddressAndEmployeeAndMarketer> list = new ArrayList<>();
        QueryWrapper<Orders>queryWrapper = new QueryWrapper<>();
        Integer total = 0;
        if(currentNum == null && wantNum == null && employeeId != null && id == null){

//            正在配送的订单
            list= ordersMapper.selectOrdersWithoutLimit(employeeId,status);
            total = list.size();
        }else if(employeeId == null && currentNum != null && wantNum != null && id == null){

//            查询所有未被抢的订单
             list= ordersMapper.selectOrdersByCondition(currentNum,null,status,wantNum,null);
             queryWrapper.lambda().eq(Orders::getStatus,2);
             total = ordersMapper.selectCount(queryWrapper);
        }else if(employeeId != null && currentNum != null && wantNum != null && id != null){

//            查询 ----- 查询
            list = ordersMapper.selectOrdersByCondition(currentNum,employeeId,4,6,id);
            total = list.size();
        }
        else {
//            员工查询已配送的订单
            list = ordersMapper.selectOrdersByCondition(currentNum, employeeId, status, wantNum, null);
            queryWrapper.lambda().eq(Orders::getStatus,4);
            queryWrapper.lambda().eq(Orders::getEmployeeId,employeeId);
            total = ordersMapper.selectCount(queryWrapper);
        }
        return new EasyUITable(total,list);
    }

    @Override
    public EasyUITable search(String id,String orderTime, Integer page1) {
        IPage page = new Page(page1,1);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(Orders::getId,id)
                .like(Orders::getOrderTime,orderTime);

        ordersMapper.selectPage(page,queryWrapper);
        Integer total = (int)page.getTotal();
        List<Orders> list = page.getRecords();
        EasyUITable easyUITable = new EasyUITable(total,list);
        return easyUITable;
    }

    @Override
    public EasyUITable getOrdersByConditionWithTime(Integer currentNum, String employeeId, Integer status, Integer wantNum, String date) {
        List<BookAddressAndEmployeeAndMarketer>list = ordersMapper.selectOrdersByConditionWithTime(currentNum,employeeId,4,6,date);
        QueryWrapper<Orders>queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Orders::getStatus,4);
        queryWrapper.lambda().like(Orders::getCompleteTime,date);
        Integer total = ordersMapper.selectCount(queryWrapper);
        return new EasyUITable(total,list);
    }

    @Override
    public EasyUITable getOrderDataById(String userId,String flag) {
        List<OrdersMarketerVo> list = ordersMapper.selectOrdersAndDish(userId,flag);
        Integer total = list.size();
        return new EasyUITable(total,list);
    }

    @Override
    public EasyUITable getTableData(Integer currentPage, Integer size) {
        IPage page = new Page(currentPage, size);
        baseMapper.selectPage(page, null);
        List<Orders> list = page.getRecords();
        Integer total = (int) page.getTotal();
        EasyUITable easyUITable = new EasyUITable(total, list);
        return easyUITable;
    }

    @Override
    public Boolean delivery(String id,String EmployeeId) {
        Orders orders = new Orders();
        orders.setStatus(3);
        orders.setEmployeeId(EmployeeId);
        orders.setId(id);
        Integer num = ordersMapper.updateById(orders);
        if(num == 1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public EasyUITable getSuspenseOrderUser(String EmployeeId) {
        List<User> list = userMapper.selectUserAboutOrders(EmployeeId);
        Integer total = list.size();
        return new EasyUITable(total,list);
    }

    @Override
    public Boolean cancelById(String id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(5);
        return ordersMapper.updateById(orders)>0;
    }
}
